"""
Case Type   : 资源池化
Case Name   : 系统函数ss_txnstatus_cache_stat函数字段及功能验证
Create At   : 2024.6
Owner       : haomeng
Description :
    1、主机查询事务信息缓存统计项
    2、备机查询事务信息缓存统计项
    3、结合聚合函数验证函数功能
Expect      :
    1、查询成功
    2、查询成功
    3、查询成功
"""

import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta_sh = CommonSH('Standby1DbUser')

        self.log.info('------检查数据库状态是否正常------')
        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.assertIn("cluster_state   : Normal", status)

    def test_innerfunc(self):
        text = '-----step1:主机查询事务信息缓存统计项; expect:查看成功-----'
        self.log.info(text)

        sql = 'select * from ss_txnstatus_cache_stat();'
        self.log.info(sql)
        res1 = self.pri_sh.execut_db_sql(sql)
        self.log.info(res1)
        self.assertIn('(1 row)', res1, '查看缓存统计信息失败')

        text = '-----step2:备机查询事务信息缓存统计项; expect:查看成功-----'
        self.log.info(text)

        res2 = self.pri_sh.execut_db_sql(sql)
        self.log.info(res2)
        self.assertIn('(1 row)', res2, '查看缓存统计信息失败')

        text = '-----step3:结合聚合函数验证函数功能; expect:查看成功-----'
        self.log.info(text)

        sql_list = ['select count(*) from ss_txnstatus_cache_stat();',
                    'select avg(vcache_gets) from ss_txnstatus_cache_stat();',
                    'select avg(vcache_gets) + avg(hcache_gets) as avg from ss_txnstatus_cache_stat();']
        self.log.info(sql_list)
        for sql in sql_list:
            res3 = self.pri_sh.execut_db_sql(sql)
            self.log.info(res3)
            self.assertIn('(1 row)', res3, '查询失误缓存信息失败')

    def tearDown(self):
        text = '-----No need to clean-----'
        self.log.info(text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
